<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>登录页</title>
    <link rel="stylesheet" href="layui/css/layui.css"/>
    <script src="layui/layui.js"></script>
    <style>
        body {
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
        }

        .login {
            width: 600px;
            height: 400px;
        }

        .login h1 {
            height: 80px;
            line-height: 80px;
            text-align: center;
        }
    </style>
</head>
<body>
<div class="login">
    <h1>用户登录页面</h1>
    <form class="layui-form" action="">
        <div class="layui-form-item">
            <label class="layui-form-label">学/工号</label>
            <div class="layui-input-block">
                <input type="text" name="userNum" required value="20230605" lay-verify="required" placeholder="请输入学/工号"
                       autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">密码</label>
            <div class="layui-input-block">
                <input type="password" name="userPwd" value="123456" required lay-verify="required" placeholder="请输入密码"
                       autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">角色</label>
            <div class="layui-input-block">
                <input type="radio" name="roleId" value="1" title="管理员" checked>
                <input type="radio" name="roleId" value="2" title="宿管">
                <input type="radio" name="roleId" value="3" title="学生">
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn" lay-submit lay-filter="loginBtn">登录</button>
                <button type="reset" class="layui-btn layui-btn-primary">重置</button>
            </div>
        </div>
    </form>
</div>

<script>
    layui.use('form', function () {
        let form = layui.form,
            layer = layui.layer;
        //监听提交
        form.on('submit(loginBtn)', function (data) {
            // layer.msg(JSON.stringify(data.field));
            let roleId = data.field.roleId;
            fetch('/user/login', {
                method: 'post',
                headers: {'Content-Type': 'application/json'},
                body: JSON.stringify(data.field)
            }).then(resp => resp.json()).then(data => {
                if (data.code === 0) {
                    layer.msg(data.msg);
                    window.location.href = 'index.html';
                } else {
                    layer.msg(data.msg);
                }
            });
            return false;
        });
    });
</script>
</body>
</html>